package com.efast.cafe.portal.controller.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.portal.bean.base.CompanyBaseLovBean;
import com.efast.cafe.portal.bean.base.CompanyRecoveryDefaultLovBean;
import com.efast.cafe.portal.bean.base.DelCompanyLovBean;
import com.efast.cafe.portal.bean.base.PortalBaseLovChildrenBean;
import com.efast.cafe.portal.bean.base.PortalBaseLovQueryBean;
import com.efast.cafe.portal.bean.company.PortalCompanyOrgBean;
import com.efast.cafe.portal.entity.base.PortalBaseLov;
import com.efast.cafe.portal.entity.base.PortalBaseParameter;
import com.efast.cafe.portal.remote.base.PortalBaseLovService;
import com.efast.cafe.portal.remote.base.PortalBaseParameterService;
import com.efast.cafe.portal.service.base.IPortalBaseLovService;
import com.efast.cafe.portal.service.base.IPortalBaseParameterService;
import com.efast.cafe.util.StringUtil;

import net.sf.json.JSONArray;

/******************************************************************************
 * @File name : BaseLovController.java
 * 
 * @Author : xl
 * 
 * @Date : 2016-6-7
 * 
 * @Copyright Notice: Copyright (c) 2016 Efast Serv. All Rights Reserved. This
 *            software is published under the terms of the Efast Software
 *            License version 1.0, a copy of which has been included with this
 *            distribution in the LICENSE.txt file.
 * 
 * 
 *            ------------------------------------------------------------------
 *            ---------- Date Who Version Comments 2016-6-7 上午11:00:07 xl 1.0
 *            Initial Version
 *****************************************************************************/
@Controller
@RequestMapping("/frame_center/base")
public class PortalBaseLovController{

	@Autowired
	private IPortalBaseLovService portalBaseLovService;
	@Autowired
	private IPortalBaseParameterService portalBaseParameterService;

	
	
	
	

	/**
	 * lov参数页面
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/lov/page")
	public @ResponseBody ResultBean lovPage( 
			HttpServletRequest request, 
			HttpServletResponse response,@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true, "");
	    Long portalBaseParameterId=null;
	    if(map.containsKey("portalBaseParameterId")){
	    	portalBaseParameterId=Long.parseLong(map.get("portalBaseParameterId").toString());
	    }
		try{
		Map<String, Object> data=portalBaseLovService.lovPage(portalBaseParameterId);
		ModelMap model=new ModelMap();
		model.addAttribute("listLov",data.get("listLov"));
		//model.addAttribute("listFirst",data.get("listFirst") );
		model.addAttribute("portalBaseParameter",data.get("portalBaseParameter"));
		model.addAttribute("listParameter",data.get("listParameter"));
		
		
			result.setObject(model);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	public static PortalBaseLovChildrenBean  getAreaTree(List<PortalBaseLovChildrenBean> list){
		// 构造tree
		Map<String, PortalBaseLovChildrenBean> roleMap=new LinkedHashMap<String, PortalBaseLovChildrenBean>();
		for (PortalBaseLovChildrenBean fsAreaTreeBean : list) {
			roleMap.put(fsAreaTreeBean.getPortal_base_lov_id().toString(), fsAreaTreeBean);
		}
		
		PortalBaseLovChildrenBean root = new PortalBaseLovChildrenBean();
		// 递归构造树
		Iterator<Map.Entry<String, PortalBaseLovChildrenBean>> it = roleMap.entrySet()
						.iterator();
		while (it.hasNext()) {
			Map.Entry<String, PortalBaseLovChildrenBean> entry = it.next();
			PortalBaseLovChildrenBean tree = entry.getValue();
			if ("0".equals(tree.getParent_id().toString())) {// 如果是根节点	
				root.getChildren().add(tree);
			} else {
				PortalBaseLovChildrenBean parent = roleMap.get(tree.getParent_id().toString());// 根据父Id查询
				if (parent != null) {
					parent.getChildren().add(tree);
				}
			}
		}
		
		return root;
	}
	/**
	 * lov参数页面
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/lov/treeList")
	public @ResponseBody ResultBean lovTree( 
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody Map<String,String> map){
		String paramKey=null;
		if(map.containsKey("paramKey")){
			paramKey=map.get("paramKey");
		}
		ResultBean result = new ResultBean(true, "");
		if(paramKey==null){
			return result;
		}
		List<PortalBaseLov> list = portalBaseLovService.getPortalBaseLovListTree(paramKey);
		PortalBaseParameter baseParameter =portalBaseParameterService.queryByParamKey(paramKey);
		
		PortalBaseLov baseLov=new PortalBaseLov();
		baseLov.setPortal_base_lov_id(0L);
		baseLov.setParam_key(paramKey);
		baseLov.setParent_id(null);
		baseLov.setOption_key(paramKey);
		baseLov.setData_type(baseParameter.getData_type());
		baseLov.setOption_desc(baseParameter.getParam_desc());
		baseLov.setModule_name(baseParameter.getModule_name());
		list.add(baseLov);
		
		List<PortalCompanyOrgBean> listResult=new ArrayList<>();
		  for(int i=0;i<list.size();i++){
          	PortalCompanyOrgBean portalCompanyOrgBean1=new PortalCompanyOrgBean();
          	portalCompanyOrgBean1.setTitle(list.get(i).getOption_desc());
          	portalCompanyOrgBean1.setData(list.get(i));
          	listResult.add(portalCompanyOrgBean1);
          }
		result.setObject(listResult);
		return result;
	}
	
	/**
	 * 企业查询参数下的自定义Lov树
	 * @param queryBean
	 * @return
	 */
	@RequestMapping(value = "/lov/list_by_company_parameter")
	public @ResponseBody ResultBean lovByCompanyParameter(@RequestBody PortalBaseLovQueryBean queryBean){
		
		ResultBean res = new ResultBean(true,"");
		PortalBaseLovQueryBean lov = portalBaseLovService.queryLovByCompanyParameterId(queryBean);
		PortalBaseParameter baseParameter =portalBaseParameterService.queryByParamKey(queryBean.getParamKey());
		if(baseParameter!=null){
			List<PortalBaseLov> list = lov.getResultList();
			PortalBaseLov baseLov=new PortalBaseLov();
			baseLov.setPortal_base_lov_id(0L);
			baseLov.setParam_key(queryBean.getParamKey());
			baseLov.setParent_id(null);
			baseLov.setData_type(baseParameter.getData_type());
			baseLov.setOption_desc(baseParameter.getParam_desc());
			baseLov.setModule_name(baseParameter.getModule_name());
			list.add(baseLov);
			
			List<PortalCompanyOrgBean> listResult=new ArrayList<>();
			for(int i=0;i<list.size();i++){
				
				PortalCompanyOrgBean portalCompanyOrgBean1=new PortalCompanyOrgBean();
	          	portalCompanyOrgBean1.setTitle(list.get(i).getOption_desc());
	          	portalCompanyOrgBean1.setData(list.get(i));
	          	listResult.add(portalCompanyOrgBean1);
				
			}

			res.setObject(listResult);
		}
		
		return res;
	}
	
	
	
	/**
	 * 用户查询参数下的自定义Lov
	 * @param queryBean
	 * @return
	 */
	@RequestMapping(value = "/lov/list_by_user_parameter")
	public @ResponseBody ResultBean lovByUserParameter(@ModelAttribute PortalBaseLovQueryBean queryBean){
		ResultBean result = new ResultBean(true, "");
		if(StringUtil.isEmpty(queryBean.getParamKey())){
			queryBean.setParamKey(queryBean.getParam_key());
		}
		result.setObject(portalBaseLovService.queryLovByUserParameterId(queryBean));
		return result;
	}

	/**
	 * 编辑操作
	 * 
	 * @Date : 2016-6-7
	 * @param baseLov
	 */
	@RequestMapping(value = "/lov/edit")
	public @ResponseBody
	ResultBean updateBaseLov(@RequestBody PortalBaseLov portalBaseLov) {
		
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.updatePortalBaseLov(portalBaseLov);
			result.setObject(portalBaseLov);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

	
	/**
	 * 操作
	 * 
	 * @Date : 2016-6-7
	 * @param baseLov
	 */
	@RequestMapping(value = "/lov/listform")
	public @ResponseBody ResultBean toLovListFormPage(
			@RequestBody Map<String,Integer> map ) {
		Long portalBaseLovId=null;
		if(map.containsKey("portalBaseLovId")){
			portalBaseLovId=Long.parseLong(map.get("portalBaseLovId").toString());
		}
		ResultBean result = new ResultBean(true, "");
		result.setObject(portalBaseLovService.getPortalBaseLovById(portalBaseLovId));
		return result;
	}
	
	
	


	/**
	 * 新增操作
	 * 
	 * @Date : 2016-6-7
	 * @param baseLov
	 */

	@RequestMapping(value = "/lov/add")
	public @ResponseBody
	ResultBean saveBaseLov(@RequestBody PortalBaseLov portalBaseLov) {
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.savePortalBaseLov(portalBaseLov);
			result.setObject(portalBaseLov.getParent_id());
			// modelAndView.setViewName("forward:/base/lov/page");
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	/**
	 * 企业编辑自定义Lov
	 * @param portalBaseLov
	 * @return
	 */
	@RequestMapping(value = "/lov/edit_by_company")
	public @ResponseBody ResultBean editCompanyLov(@RequestBody CompanyBaseLovBean data){
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.updateCompanyLov(data);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	/**
	 * 企业删除自定义lov
	 * @param portalBaseLov
	 * @return
	 */
	@RequestMapping(value = "/lov/del_by_company")
	public @ResponseBody ResultBean delCompanyLov(@RequestBody DelCompanyLovBean bean){
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.delCompanyLov(bean);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	/**
	 * 企业恢复平台默认lov
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "/lov/recoveryDefault_company")
	public @ResponseBody ResultBean recoveryDefault(@RequestBody CompanyRecoveryDefaultLovBean bean){
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.recoveryDefault(bean);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}


	
	
	
	/**
	 * 用户添加自定义Lov
	 * @param portalBaseLov
	 * @return
	 */
	@RequestMapping(value = "/lov/add_by_user")
	public @ResponseBody ResultBean addUserLov(@RequestBody PortalBaseLov portalBaseLov){
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.addUserLov(portalBaseLov);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	
	@RequestMapping(value = "/lov/add_by_company")
	public @ResponseBody ResultBean addCompanyLov(@RequestBody CompanyBaseLovBean data){
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.addCompanyLov(data);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	/**
	 * 用户编辑自定义Lov
	 * @param portalBaseLov
	 * @return
	 */
	@RequestMapping(value = "/lov/edit_by_user")
	public @ResponseBody ResultBean editUserLov(@RequestBody PortalBaseLov portalBaseLov){
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.updateUserLov(portalBaseLov);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	/**
	 * 用户删除自定义lov
	 * @param portalBaseLov
	 * @return
	 */
	@RequestMapping(value = "/lov/del_by_user")
	public @ResponseBody ResultBean delUserLov(
			@RequestBody Map<String,Integer> map){
		ResultBean result = new ResultBean(true, "");
		try{
			if(map.containsKey("portal_base_lov_id")){
				Integer id=map.get("portal_base_lov_id");
				portalBaseLovService.delUserLov(id);
			}
			
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	

	
	/**
	 * 删除操作
	 * 
	 * @Date : 2016-6-7
	 * @param basLovId
	 */

	@RequestMapping(value = "/lov/del")
	public @ResponseBody
	ResultBean deleteBaseLov(@RequestBody Map<String,Integer> map) {
		Long portalBasLovId=null;
		if(map.containsKey("portalBasLovId")){
			portalBasLovId=Long.parseLong(map.get("portalBasLovId").toString());
		}
		ResultBean result = new ResultBean(true, "");
		try{
			portalBaseLovService.deletePortalBaseLov(portalBasLovId);
		}catch(Exception e){
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}

}
